package org.shj.pattern.observe

import scala.collection.mutable.ArrayBuffer

abstract class Subject {
  val observers = ArrayBuffer[Observer]()
  
  def addObserver(ob: Observer) {
    observers += ob
  }
  
  def removeObserver(ob: Observer){
    observers -= ob
  }
  
  def notifyObservers(){
    observers.foreach(_.update())
  }
}

class Stock extends Subject{
  private var price: Double = _
  
  def setPrice(newPrice: Double){
    if(price != newPrice){
      price = newPrice
      notifyObservers()
    }
  }
  
  def getPrice(): Double ={
    price
  }
}